<?php
function list_depart_option($depart,$con){
	$q =  "select * from view_depart_hier where depart_cancel='f';";
	$ans = getPG($q);
	$node = findRoot($ans);
	if($node==-1) return;
	foreach($node as $data){
		list_depart_option_recur($depart,$ans,$data,0,$con);		
	}
}

function findRoot($dat){
	$count = 0;		
	foreach($dat as $data){
		if($data['head_depart_id']==$data['under_depart_id']){
			$result[$count] = $data;
			$count = $count + 1;
		}
	}
	if($count==0) return -1;
	return $result;
}
	
function list_depart_option_recur($depart,$ans,$under,$level,$con){
	$prefix = "";		
	if($con)	
		for($i=0;$i<$level;$i++) $prefix = $prefix.'===';
	if($under['under_depart_id']==$depart){
		echo "<option value=\"".$under['under_depart_id']."\" selected>".$prefix.$under['under_name']."</option>\n";	
	}
	else{
		if($con)
			echo "<option value=\"".$under['under_depart_id']."\" >".$prefix.$under['under_name']."</option>\n";
	}
	foreach($ans as $dat){
		if(($dat['head_depart_id']==$under['under_depart_id'])&&($dat['head_depart_id']!=$dat['under_depart_id']))
			list_depart_option_recur($depart,$ans,$dat,$level+1,$con);
	}
}

function list_department_user($depart,$form){
	echo "<select name=\"depart_id\" size=\"1\" onchange=\"document.getElementById('".$form."').submit()\">\n";

	if($_SESSION['edoc_write']==1){
		list_depart_option($_SESSION['edoc_depart_id'],false);
	}
	else if($depart==0 & $_SESSION['edoc_write']==2){
		list_depart_option($_SESSION['edoc_depart_id'],true);
	}
	else if($depart!=0 & $_SESSION['edoc_write']==2){
		list_depart_option($depart,true);
	}

	echo "</select>\n";
}

function list_department_userR($depart,$form){
	echo "<select name=\"depart_id\" size=\"1\" onchange=\"document.getElementById('".$form."').submit()\">\n";

	if($_SESSION['edoc_read']==1){
		list_depart_option($_SESSION['edoc_depart_id'],false);
	}
	else if($depart==0 & $_SESSION['edoc_read']==2){
		list_depart_option($_SESSION['edoc_depart_id'],true);
	}
	else if($depart!=0 & $_SESSION['edoc_read']==2){
		list_depart_option($depart,true);
	}

	echo "</select>\n";
}

function list_department_userX($depart){

	if($_SESSION['edoc_write']==1){
		$q = "select * from department where depart_id=".$_SESSION['edoc_depart_id'].";";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']){
				echo "<input type=\"hidden\" name=\"depart_id\" value=\"".$dat['depart_id']."\">\n";			 
				echo $dat['depart_name'];
			 }
		}
	}
	else if($depart==0 & $_SESSION['edoc_write']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$_SESSION['edoc_depart_id']){
				echo "<input type=\"hidden\" name=\"depart_id\" value=\"".$dat['depart_id']."\">\n";			 
				echo $dat['depart_name'];
			}
		}
	}
	else if($depart!=0 & $_SESSION['edoc_write']==2){
		$q = "select * from department;";
		$ans = getPG($q);
		foreach($ans as $dat){
			if($dat['depart_id']==$depart){
				echo "<input type=\"hidden\" name=\"depart_id\" value=\"".$dat['depart_id']."\">\n";			 
				echo $dat['depart_name'];
			}
		}
	}

	echo "</select>\n";
}

function list_department_userS($depart){
	echo "<select name=\"depart_id_recieve\" size=\"1\" onchange=\"doctypech()\" id=\"xdepart\">\n";
	if($_SESSION['edoc_read']==1){
		list_depart_option($_SESSION['edoc_depart_id'],false);
	}
	else if($_SESSION['edoc_read']==2){
	    echo "<option value=\"0\" >ทั้งหมด</option>";
		list_depart_option($_SESSION['edoc_depart_id'],true);
	}

	echo "</select>\n";
}

function list_department_userSsent($depart){
	echo "<select name=\"depart_id_sent\" size=\"1\" onchange=\"doctypech()\" id=\"xdepart\">\n";
	if($_SESSION['edoc_read']==1){
		list_depart_option($_SESSION['edoc_depart_id'],false);
	}
	else if($_SESSION['edoc_read']==2){
	    echo "<option value=\"0\" >ทั้งหมด</option>";
		list_depart_option($_SESSION['edoc_depart_id'],true);
	}

	echo "</select>\n";
}

function list_department_userK(){
	echo "<select name=\"depart_id_ksean\" size=\"1\" onchange=\"positionch()\" id=\"xdepart2\">\n";
    echo "<option value=\"0\" >---</option>";
	list_depart_option(0,true);
	echo "</select>\n";
}

function dateThEnco($d){
	$a = explode("/",$d);
	if(count($a)==3){
		$a[2] = $a[2]-543;
		$aa = $a[1]."/".$a[0]."/".$a[2];
		return $aa;
	}else{
		return $d;
	}
}

function dateThDeco($d){
	$a = explode("-",$d);
	if(count($a)==3){
		$a[0] = $a[0]+543;
		$aa = $a[2]."/".$a[1]."/".$a[0];
		return $aa;
	}else{
		return $d;
	}
}

function shEdocType($doctype,$doctype_add){
	if($doctype!=0){
		$q = "select edoc_type_name from edoc_type where edoc_type_id=$doctype;";
		$ans = getPG($q);
		return $ans[0]['edoc_type_name'];
	}else{
		return trim($doctype_add);
	}
}

function shEdocTypeSent($doctypesent,$doctypesent_add)
{
	if($doctypesent!=0){
		$q = "select edoc_typesent_name from edoc_type_sent where edoc_typesent_id=$doctypesent;";
		$ans = getPG($q);
		return $ans[0]['edoc_typesent_name'];
	}else{
		return trim($doctypesent_add);
	}
}

function shDepartSend($depart_send_id,$depart_send_name_out){
	if($depart_send_id>1){
		$q = "select depart_name from department where depart_id=$depart_send_id;";
		$ans = getPG($q);
		return $ans[0]['depart_name'];
	}else if($depart_send_id==1) return "ภายนอก { ".trim($depart_send_name_out)." }";
	else return trim($depart_send_name_out);
}

function shDepartSendFast($depart_send_id,$dname_send,$depart_send_name_out){
	if($depart_send_id==1) return "ภายนอก { ".$depart_send_name_out." }";
	else return $dname_send;
}

function xshDepartSendFast($depart_send_id,$dname_send,$depart_send_name_out){
	if($depart_send_id==1){
	    if($depart_send_name_out!="") return $depart_send_name_out;
        else return "ภายนอก";
	}
	else return $dname_send;
}

function practise($edoc_id,$depart_id,$txt){
    //if($txt!="") $comment= $txt."\n----------------\n";
    $ans = "";
    $ksean = getPG("select * from view_ksean where edoc_id=$edoc_id and depart_id=$depart_id;");
    for($i=0;$i<count($ksean);$i++){
        $ans = $ans.$ksean[$i]['ksean_txt']."\n".$ksean[$i]['pname_name']."\n(".$ksean[$i]['position_name'].")\n".dateThDeco($ksean[$i]['ksean_date']);
        if(isset($ksean[$i+1])) $ans=$ans."\n\n";
    }
	if($ans!="") $comment = $txt."\n----------------\n";
	else $comment = $txt."\n\n";
    return $comment.$ans;
}

function practise2($edoc_id,$depart_id,$txt){
    //if($txt!="") $comment= $txt."\n----------------\n";
    $ans = "";
    $ksean = getPG("select * from view_ksean where edoc_id=$edoc_id order by edoc_id,ksean_date;");
    for($i=0;$i<count($ksean);$i++){
        $ans = $ans.$ksean[$i]['ksean_txt']."\n".$ksean[$i]['pname_name']."\n(".$ksean[$i]['position_name'].")\n".dateThDeco($ksean[$i]['ksean_date']);
        if(isset($ksean[$i+1])) $ans=$ans."\n\n";
    }
	if($ans!="") $comment = $txt."\n----------------\n";
	else $comment = $txt."\n\n";
    return $comment.$ans;
}

function shPDFname($pdf){
	return $pdf['name'];
}

function PDFnameLink($pdf_name,$edoc_id){
    if($pdf_name!="")
        echo "<a href=\"loadpdf.php?edoc_id=$edoc_id\" target=\"_blank\" >$pdf_name</a>";
}

function get_depart_doctype($doctype){
	$ans = getPG("select * from view_edoc_type where edoc_type_id=$doctype;");
	if($ans==FALSE){
		return -1;
	}
	return $ans[0];
}

function canEditDoc($dat,$depart_id){
    if($_SESSION['edoc_write']==2) return true;
    else if(($_SESSION['edoc_write']==1) && ($dat['depart_owner_id']==$_SESSION['edoc_depart_id']) && ($depart_id==$_SESSION['edoc_depart_id'])) return true;
    else if(($_SESSION['edoc_write']==1) && ($dat['depart_id_recieve']==$_SESSION['edoc_depart_id']) && ($dat['depart_owner_id']!=$_SESSION['edoc_depart_id'])) return true;
    return false;
}

function canChangeDoc($doc){
    if(($_SESSION['edoc_write']==1) && ($doc['depart_id_recieve']==$_SESSION['edoc_depart_id']) && ($dat['depart_owner_id']!=$_SESSION['edoc_depart_id'])) return true;
    return false;
}

function canRecieveDoc($depart_id){
    if($_SESSION['edoc_write']==2) return true;
    else if(($_SESSION['edoc_write']==1) && ($_SESSION['edoc_depart_id']==$depart_id)) return true;
    return false;
}

function canReadDoc($dat,$depart_id){
    if($dat['pdf_name']!=""){
        if($_SESSION['edoc_read']==2) return true;
        else if(($_SESSION['edoc_read']==1) && ($depart_id==$_SESSION['edoc_depart_id'])) return true;
    }
    return false;
}

function canReadKseanDoc($depart_id){
    if($_SESSION['edoc_read']==2) return true;
    else if(($_SESSION['edoc_read']==1) && ($depart_id==$_SESSION['edoc_depart_id'])) return true;
    return false;
}

function canWriteKseanDoc($edoc_id,$depart_id){
    if($_SESSION['edoc_write']==2) return true;
    else if(($_SESSION['edoc_write']==1) && ($depart_id==$_SESSION['edoc_depart_id']) && ((isDocInDepart($edoc_id,$depart_id) || isDocSentDepart($edoc_id,$depart_id)))) return true;
    return false;
}

function canWriteKseanDocKsean($ksean_id,$depart_id){
	if($_SESSION['edoc_write']==2) return true;
	else if(isKseanDepart($ksean_id,$depart_id)) return true;
	return false;
}

function isKseanDepart($ksean_id,$depart_id){
	$q = "select * from view_ksean where ksean_id=$ksean_id and depart_id=$depart_id;";
	$ans = getPG($q);
	if(isset($ans[0])) return true;
	return false;
}

function canLoadPDF($edoc_id){
    if($_SESSION['edoc_read']==2) return true;
    else if(($_SESSION['edoc_read']==1) && (isInDocInList($edoc_id,$_SESSION['edoc_depart_id']) || isInDocRecieve($edoc_id,$_SESSION['edoc_depart_id']) || isInDocInListSent($edoc_id,$_SESSION['edoc_depart_id']))) return true;
    return false;
}

function isInDocInList($edoc_id,$depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_recieve where edoc_id not in (select edoc_id from view_run_recieve where depart_id_run=$depart_id and depart_id_recieve=$depart_id) and depart_id_run=$depart_id and depart_owner_id=depart_id_recieve and edoc_id=$edoc_id;");
    else
        $ans = getPG("select * from view_run_recieve where (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") and edoc_id not in (select edoc_id from view_run_recieve where depart_id_run=$depart_id and depart_id_recieve=$depart_id) and depart_id_run=$depart_id and depart_owner_id=depart_id_recieve and edoc_id=$edoc_id;");
    
    if(!isset($ans[0])) return false;
    return true;
}

function isInDocInListSent($edoc_id,$depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
//        $ans = getPG("select * from view_run_sent where edoc_id not in (select edoc_id from view_run_sent where depart_id_run=$depart_id and depart_id_sent=$depart_id) and depart_id_run=$depart_id and depart_owner_id=depart_id_sent and edoc_id=$edoc_id;");
        $ans = getPG("select * from view_run_sent where  depart_owner_id=depart_id_sent and edoc_id=$edoc_id;");
    else
        $ans = getPG("select * from view_run_sent where (secret<=$secret_u or depart_id_sent=".$_SESSION['edoc_depart_id'].") and  depart_owner_id=depart_id_sent and edoc_id=$edoc_id;");
    
    if(!isset($ans[0])) return false;
    return true;
}


function isInDocRecieve($edoc_id,$depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_recieve where depart_id_recieve=$depart_id and edoc_id=$edoc_id;");
    else
        $ans = getPG("select * from view_run_recieve where (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") and depart_id_recieve=$depart_id and edoc_id=$edoc_id;");
	if(!isset($ans[0])) return false;
    return true;
}

function isDocInDepart($edoc_id,$depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_recieve where depart_id_run=$depart_id and edoc_id=$edoc_id;");
    else
        $ans = getPG("select * from view_run_recieve where (secret<=$secret_u or depart_id_run=".$_SESSION['edoc_depart_id'].") and depart_id_run=$depart_id and edoc_id=$edoc_id;");
	if(!isset($ans[0])) return false;
    return true;
}

function isDocSentDepart($edoc_id,$depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_edoc_sent where depart_id=$depart_id and edoc_id=$edoc_id;");
	if(!isset($ans[0])) return false;
    return true;
}

function shSecret($secret){
	if($secret==0) return "ปกติ";
	else if($secret==1) return "ลับ";
	else if($secret==2) return "ลับมาก";
	else if($secret==3) return "ลับที่สุด";
}

function shRapid($rapid){
	if($rapid==0) return "ปกติ";
	else if($rapid==1) return "ด่วน";
	else if($rapid==2) return "ด่วนมาก";
	else if($rapid==3) return "ด่วนที่สุด";
}

function secretGroup($secret){
    for($i=0;$i<4;$i++){
        if($i==$secret)
            echo "<input type=\"radio\" name=\"secret\" value=\"$i\" checked >".shSecret($i)." &nbsp;&nbsp;&nbsp;";
        else
            echo "<input type=\"radio\" name=\"secret\" value=\"$i\" >".shSecret($i)." &nbsp;&nbsp;&nbsp;";
    }
}

function rapidGroup($rapid){
    for($i=0;$i<4;$i++){
        if($i==$rapid)
            echo "<input type=\"radio\" name=\"rapid\" value=\"$i\" checked >".shRapid($i)." &nbsp;&nbsp;&nbsp;";
        else
            echo "<input type=\"radio\" name=\"rapid\" value=\"$i\" >".shRapid($i)." &nbsp;&nbsp;&nbsp;";
    }
}

function getRecieveNo($depart_id){
    $ans = getPG("select * from depart_recieve_seq_".$depart_id.";");
    $last = $ans[0]['last_value'];
    $called = $ans[0]['is_called'];
    if($called=='t') $last = $last+1;
    return $last;
}

function setRecieveNo($depart_id,$recieve_no){
    updatePG("select setval('depart_recieve_seq_".$depart_id."',$recieve_no,false);");
}

function setSentNo($depart_id,$sent_no,$doctypesent){
	if($doctypesent==0){
		$doctypeall = getPG("select * from edoc_type_sent where depart_id=$depart_id;");
		foreach($doctypeall as $dat)
			updatePG("select setval('depart_sent_seq_".$dat['edoc_typesent_id']."',$sent_no,false);");
	}else updatePG("select setval('depart_sent_seq_".$doctypesent."',$sent_no,false);");
}

function getSentNo($doctypesent){
	if($doctypesent==0) return "---";
    $ans = getPG("select * from depart_sent_seq_".$doctypesent.";");
    $last = $ans[0]['last_value'];
    $called = $ans[0]['is_called'];
    if($called=='t') $last = $last+1;
    return $last;
}

function sendStatus($edoc_id){
    $ans = getPG("select * from view_edoc_run where edoc_id=".$edoc_id.";");
    if(!isset($ans[0])) return "สิ้นสุดการส่งต่อ";
    $r = "";
    foreach($ans as $dat){
        $r = $r.$dat['depart_name']."<br />";
    }
    return $r;
}
?>
